// src/pages/index/index.js
import { I18n } from "@miniprogram-i18n/core";

const app = getApp();

Page({
  behaviors: [I18n],
  data: {
    stageModel: {
      records: [],
      sumAmount: 0,
    },
    recordModel: {
      baseAmount: 0,
      times: 0,
      isPositive: true,
      sumAmount: 0,
    },
    stages: [],
    currentStage: null,
    currentRecord: null,
    baseAmount: 1,
    recordCalculatedResult: 0,
    sumAmount: 0,
    message: "",
  },

  onLoad: function () {
    this.newStage();
    this.newRecord();
  },

  onRestart: function () {
    this.setData({
      stageModel: {
        records: [],
        sumAmount: 0,
      },
      recordModel: {
        baseAmount: 0,
        times: 0,
        isPositive: true,
        sumAmount: 0,
      },
      stages: [],
      currentStage: null,
      currentRecord: null,
      baseAmount: 1,
      recordCalculatedResult: 0,
      sumAmount: 0,
      message: "",
    });
    this.newStage();
    this.newRecord();
  },

  onNewStage: function () {
    this.newStage();
    this.newRecord();
  },

  newRecord: function () {
    this.setData({
      currentRecord: Object.assign({}, this.data.recordModel),
    });
    this.data.currentStage.records.push(this.data.currentRecord);
    this.setData({
      currentStage: this.data.currentStage,
    });
  },

  newStage: function () {
    this.data.stageModel.records = [];
    this.setData({
      currentStage: Object.assign({}, this.data.stageModel),
    });
    this.data.stages.push(this.data.currentStage);
    this.setData({
      stages: this.data.stages,
    });
  },

  bindBaseAmountInput: function (e) {
    this.setData({
      baseAmount: e.detail.value,
    });
  },

  onWin1TimeTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 1,
        isPositive: true,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onWin2TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 2,
        isPositive: true,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onWin3TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 3,
        isPositive: true,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onWin4TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 4,
        isPositive: true,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onWin5TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 5,
        isPositive: true,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onLose1TimeTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 1,
        isPositive: false,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onLose2TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 2,
        isPositive: false,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onLose3TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 3,
        isPositive: false,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onLose4TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 4,
        isPositive: false,
      },
    });
    this.updateRecordCalculatedResult();
  },

  onLose5TimesTap: function () {
    this.setData({
      currentRecord: {
        baseAmount: this.data.baseAmount,
        times: 5,
        isPositive: false,
      },
    });
    this.updateRecordCalculatedResult();
  },

  updateRecordCalculatedResult: function () {
    this.setData({
      recordCalculatedResult: this.calculateCurrentRecordResult(),
    });
  },

  calculateCurrentRecordResult: function () {
    let record = this.data.currentRecord;
    return this.calculateRecordResult(record);
  },

  calculateRecordResult: function (record) {
    return record.baseAmount * record.times * (record.isPositive ? 1 : -1);
  },

  onCompleteRecordTap: function () {
    // check
    if (this.data.recordCalculatedResult === 0) {
      this.setData({
        message: app.globalData.i18n.t(
          "pages.index.message.NeedAddOrMinusBeforeCaculate"
        ),
      });
      wx.showToast({
        icon: "none",
        title: this.data.message,
      });
      return;
    }
    if (this.data.baseAmount <= 0) {
      this.setData({
        message: app.globalData.i18n.t(
          "pages.index.message.BaseAmountRequireNotLessThanZero"
        ),
      });
      wx.showToast({
        icon: "none",
        title: this.data.message,
      });
      return;
    }

    // sync
    let records = this.data.currentStage.records;
    let lastRecord = records[records.length - 1];
    lastRecord.baseAmount = this.data.currentRecord.baseAmount;
    lastRecord.times = this.data.currentRecord.times;
    lastRecord.isPositive = this.data.currentRecord.isPositive;
    lastRecord.sumAmount = this.data.recordCalculatedResult;
    let stages = this.data.stages;
    let lastStage = stages[stages.length - 1];
    lastStage.records = records;
    let stageSumAmount = 0;
    for (let i = 0; i < lastStage.records.length; i++) {
      const record = lastStage.records[i];
      stageSumAmount += record.sumAmount;
    }
    lastStage.sumAmount = stageSumAmount;
    let sumAmount = 0;
    for (let j = 0; j < stages.length; j++) {
      const stage = stages[j];
      sumAmount += stage.sumAmount;
    }
    this.setData({
      stages: stages,
      recordCalculatedResult: 0,
      message: "",
      sumAmount: sumAmount,
    });
    if (this.data.currentRecord.times === 4) {
      this.newStage();
    }
    this.newRecord();
  },
});
